package resourcemanagement;

import java.util.HashMap;

/**
 * Container class for all the ManagedResource<T> instances
 * @author SwiftPengu
 *
 */
public class ResourceManager<T> {
	private final HashMap<String,ManagedResource<T>> resources = new HashMap<String,ManagedResource<T>>();
	
	/**
	 * Adds 
	 * @param name
	 * @param resource
	 */
	public void addResource(String name, ManagedResource<T> resource){
		resources.put(name, resource);
	}
	
	/**
	 * Looks up the resource associated with the given name
	 * @param name the name of the resource to search for
	 * @return null if not found, else return the resource
	 */
	public ManagedResource<T> findResource(String name){
		return resources.get(name);
	}
	
	/**
	 * Removes the resource with the given name, if it exists. Unloads the resource if nessecary
	 * @param name the name of the resource which is to be removed
	 * @return true if the resource was found and deleted
	 */
	public boolean removeResource(String name){
		boolean result = true;
		ManagedResource<T> mr = findResource(name);
		if(mr==null)result = false; else{
			if(mr.isLoaded())mr.unload();
			resources.remove(name);
		}
		return result;
	}
}
